package com.zjf.fupload;

import java.util.StringTokenizer;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.zjf.fupload.popup.actions.RecentPathList;

/**
 * @author zjf
 */
public class FUploadPlugin extends AbstractUIPlugin {
	
	public static final String PROJECT_NAME = "com.zjf.fupload";
	
	public static final String NAME = "fupload";

	//The shared instance.
    private static FUploadPlugin plugin;

    //The identifiers for the preferences 
    public static final String SERVERS_PREFERENCE = "fservers";
    
    public static final String HISTORY_PATH_LIST_PREFERENCE = "pathlist";

    //The default values for the preferences
    public static final String DEFAULT_SERVERS = "测试:ftp.cc.ac.cn:21:anonymous:123:/pub;W唯品会:172.18.21.10:21:live800:4gnlhr6q:/vipshop.live800.com/webapps/;" +
    		"H华为:172.18.21.10:21:live800:4gnlhr6q:/vmall.dev.live800.com/webapps/;" +
    		"X迅雷:172.18.21.10:21:live800:4gnlhr6q:/xunlei.dev.live800.com/webapps/;" +
    		"Ccare3:172.18.21.10:21:live800:4gnlhr6q:/care3.dev.live800.com/webapps/";
    
    /**
     * 历史路径列表/
     */
    public static final RecentPathList historyPathList = new RecentPathList();

	public static final String PREFERENCE_DELIMITER = ";";

    public FUploadPlugin() {
           super();
           plugin = this;
    }

    public static FUploadPlugin getDefault() {
           return plugin;
    }

    /** 
     * Initializes a preference store with default preference values 
     * for this plug-in.
     */
    protected void initializeDefaultPreferences(IPreferenceStore store) {
    	store.setDefault(SERVERS_PREFERENCE, DEFAULT_SERVERS);
    	store.setDefault(HISTORY_PATH_LIST_PREFERENCE, historyPathList.toString());
    }
    
    public String[] getDefaultServersPreference(){
        return convert(getPreferenceStore().getDefaultString(SERVERS_PREFERENCE));
    }
 
	/**
	 * Returns the bad words preference.
	 */
	public String[] getServersPreference() {
	        return convert(getPreferenceStore().getString(SERVERS_PREFERENCE));
	}
	
	
	        
	/**
	 * Converts PREFERENCE_DELIMITER delimited String to a String array.
	 */
	private String[] convert(String preferenceValue) {
	        StringTokenizer tokenizer =
	               new StringTokenizer(preferenceValue, PREFERENCE_DELIMITER);
	        int tokenCount = tokenizer.countTokens();
	        String[] elements = new String[tokenCount];
	        for (int i = 0; i < tokenCount; i++) {
	               elements[i] = tokenizer.nextToken();
	        }
	 
	        return elements;
	}
	 
	/**
	 * Sets the bad words preference.
	 */
	public void setBadWordsPreference(String[] elements) {
	        StringBuffer buffer = new StringBuffer();
	        for (int i = 0; i < elements.length; i++) {
	               buffer.append(elements[i]);
	               buffer.append(PREFERENCE_DELIMITER);
	        }
	        getPreferenceStore().setValue(SERVERS_PREFERENCE, buffer.toString());
	}
	
	/**
	 * Sets the history path list preference.
	 */
	public void updateHistoryPathList(String path) {
		String historyPaths = getPreferenceStore().getString(HISTORY_PATH_LIST_PREFERENCE);
		RecentPathList rpl = RecentPathList.parse(historyPaths);
		rpl.add(path);
	    getPreferenceStore().setValue(HISTORY_PATH_LIST_PREFERENCE, rpl.toString());
	}
	
	/**
	 * Sets the bad words preference.
	 */
	public RecentPathList getHistoryPathList() {
		String historyPaths = getPreferenceStore().getString(HISTORY_PATH_LIST_PREFERENCE);
		RecentPathList rpl = RecentPathList.parse(historyPaths);
		return rpl;
	}
	
}
